package com.taobao.askwho.utils;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

public class DateUtil {
	private static  Logger logger = Logger.getLogger(DateUtil.class);
	
	final static public String FORMAT_STRING_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	final static public String FORMAT_STRING_YMD = "yyyy-MM-dd";
	
	public static String getStringOfSqlDate(java.sql.Date sqlDate) {
		return getStringOfSqlDate(sqlDate, FORMAT_STRING_YMDHMS);
	}
	
	public static String getStringOfSqlDate(java.sql.Date sqlDate, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			String time = sdf.format(sqlDate);
			return time;
		} catch (Exception e) {
			logger.error("", e);
		}
		return "0000-00-00 00:00:00";
	}
	
	public static String getStringOfUtilDate(java.util.Date utilDate) {
		return getStringOfUtilDate(utilDate, FORMAT_STRING_YMD);
	}
	
	public static String getStringOfUtilDate(java.util.Date utilDate, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			String time = sdf.format(utilDate);
			return time;
		} catch (Exception e) {
			logger.error("", e);
		}
		return "0000-00-00";
	}
	
	/**
	 *	eg:2012-07-21 13:12:00.0 -> 2012-07-21 13:12:00 
	 */
	public static String cutLastNumOfSqlTimeString(String timeStr) {
		if(timeStr == null)
			return "0001-01-01 01:01:01";
		return timeStr.substring(0,timeStr.lastIndexOf('.'));
	}
	
	public static void main(String[] args) {
		System.out.println(new Date(1231231));
		System.out.println(DateUtil.getStringOfSqlDate(new Date(1231231)));
	}
}
